﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace _01_ObserverModelBasic
{
    /// <summary>
    /// 前台秘书类
    /// </summary>
    class Secreatary
    {
        public string SecretaryAction { get; set; }   // 通知的事

        private IList<StockObserver> observerList = new List<StockObserver>(); // 观察者同事列表

        /// <summary>
        /// 添加要通知的同事
        /// </summary>
        /// <param name="observer"></param>
        public void Attach(StockObserver observer)
        {
            observerList.Add(observer);
        }

        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            foreach (StockObserver item in observerList)
            {
                item.Update();
            }
        }
    }
    /// <summary>
    /// 股票观察者
    /// </summary>
    class StockObserver
    {
        public StockObserver(string name, Secreatary sub)
        {
            this.name = name;
            this.sub = sub;
        }

        private string name;  // 名字
        private Secreatary sub; // 前台秘书

        public void Update()
        {
            Console.WriteLine($"{sub.SecretaryAction},{name}关闭股票行情，继续工作！");
        }
       

    }
    class Program
    {
        static void Main(string[] args)
        {
            Secreatary secreatary = new Secreatary();
            // 看股票的同事
            StockObserver stockObserver1 = new StockObserver("为观察",secreatary);
            StockObserver stockObserver2 = new StockObserver("一观察", secreatary);
            // 前台记下要通知的同事
            secreatary.Attach(stockObserver1);
            secreatary.Attach(stockObserver2);
            // 发现老板来了
            secreatary.SecretaryAction = "老板来了";
            // 通知
            secreatary.Notify();
            Console.Read();
        }
    }
}
